using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using DAL;
using Utility;
using System.IO;

public partial class sanphamsua : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            LoadDDL();
            LoadData();
            
        }
    }
    private void LoadData()
    {

        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbsanphamDTO sp = tbsanphamBUS.GetByID(id);
            if (sp != null)
            {
                txt_Tensp.Text = sp.Tensp;
                txt_dacdiem.Text = sp.Dacdiem;
                txt_thongtin.Text = sp.Thongtin;
                txt_thoathuan.Text = sp.Thoathuan;
                txt_giaban.Text = sp.Giaban.ToString();
                txt_soluong.Text = sp.Soluongmax.ToString();
                ddl_Loai.SelectedValue = sp.Maloaisp.ToString();
                tbhinhanhDTOColection hinhanh = tbhinhanhBUS.GETAll();
                DatePicker1.SelectedDate = sp.Ngaybatdau;
                DatePicker2.SelectedDate = sp.Ngayketthuc;
                ddl_dn.SelectedValue = sp.Madn.ToString();
                ddl_tinhthanh.SelectedValue = sp.Matinhthanh.ToString();
                txt_giagoc.Text = sp.Giagoc.ToString();
                txt_soluongdaban.Text = sp.Soluongdaban.ToString();
                txt_gtsd.Text = sp.Giatrisudung.ToString();
                txt_chietkhau.Text = sp.Chietkhau.ToString();
                ddl_trangthai.SelectedValue = sp.Matrangthai.ToString();
                hinh.ImageUrl = "~/Admin/image/hinhnho/" + hinhanh[0].Tenhinhanh.ToString();
                hinh.FullImageURL = "~/Admin/image/thump/" +hinhanh[0].Tenhinhanh.ToString();
                //fu_HinhMinhHoa.ImageUrl = "~/Admin/hinhanh/" + sp.Hinhanh;
                
                //DatePicker1.SelectedDate =DateTime.Parse(sp.Ngaynhapve.ToString()) ;
                
                
               

            }
        }
        catch
        {
            Response.Redirect("sanphamlist.aspx");
        }




    }
    private void LoadDDL()
    {
        ddl_Loai.DataSource = tbloaisanphamBUS.GETAll();
        ddl_Loai.DataTextField = tbloaispDTOColumn.Tenloaisp.ToString();
        ddl_Loai.DataValueField = tbloaispDTOColumn.Maloaisp.ToString();
        ddl_Loai.DataBind();

        ddl_dn.DataSource = tbdoanhnghiepBUS.GETAll();
        ddl_dn.DataTextField = tbdoanhnghiepDTOColumn.Tendn.ToString();
        ddl_dn.DataValueField = tbdoanhnghiepDTOColumn.Madn.ToString();
        ddl_dn.DataBind();
        ddl_trangthai.DataSource = tbtrangthaiBUS.GETAll();
        ddl_trangthai.DataTextField = tbtrangthaiDTOColumn.Tentrangthai.ToString();
        ddl_trangthai.DataValueField = tbtrangthaiDTOColumn.Matrangthai.ToString();
        ddl_trangthai.DataBind();
        ddl_tinhthanh.DataSource = tbtinhthanhBUS.GETAll();
        ddl_tinhthanh.DataTextField =tbtinhthanhDTOColumn.Tentinhthanh.ToString();
        ddl_tinhthanh.DataValueField = tbtinhthanhDTOColumn.Matinhthanh.ToString();
        ddl_tinhthanh.DataBind();
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        try
        {
            int k = int.Parse(Request.QueryString["id"].ToString());
            tbsanphamDTO sanpham = tbsanphamBUS.GetByID(k);
            sanpham.Tensp = txt_Tensp.Text;
            sanpham.Dacdiem = txt_dacdiem.Text;
            sanpham.Giaban = float.Parse(txt_giaban.Text);
            sanpham.Giagoc = float.Parse(txt_giagoc.Text);
            if (fu_HinhMinhHoa.HasFile)
            {
                //File.Delete(Server.MapPath("~/Admin/image/hinhnho/" + sanpham.Hinhanh.ToString()));
                //File.Delete(Server.MapPath("~/Admin/image/thump/" + sanpham.Hinhanh.ToString()));


                //string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.PostedFile.FileName;
                //fu_HinhMinhHoa.PostedFile.SaveAs(Server.MapPath("~/Admin/image/thump/") + filename);
                //System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Admin/image/thump/") + filename);
                //ImageUtility.MakeThumbnailPhoto(img, 70, 70, Server.MapPath("~/Admin/image/hinhnho/"), filename);
                //sanpham.Hinhanh = filename;
            }
            else
            {
                //sanpham.Hinhanh = sanpham.Hinhanh.ToString();
            }
            sanpham.Giatrisudung = int.Parse(txt_gtsd.Text.ToString());
            sanpham.Thongtin = txt_thongtin.Text;
            sanpham.Thoathuan = txt_thoathuan.Text;
            sanpham.Maloaisp = int.Parse(ddl_Loai.SelectedValue);
            sanpham.Soluongmax = int.Parse(txt_soluong.Text.ToString());
            sanpham.Soluongdaban = int.Parse(txt_soluongdaban.Text.ToString());
            sanpham.Madn = int.Parse(ddl_dn.SelectedValue);
            sanpham.Matrangthai = int.Parse(ddl_trangthai.SelectedValue);
            sanpham.Matinhthanh = int.Parse(ddl_tinhthanh.SelectedValue);
            sanpham.Chietkhau = int.Parse(txt_chietkhau.Text.ToString());
            sanpham.Ngaybatdau = DatePicker1.SelectedDate;
            sanpham.Ngayketthuc = DatePicker2.SelectedDate;
            tbsanphamBUS.Update(sanpham);
            Label_thanhcong.Text = tbsanphamBUS.error;
            Response.Redirect("sanphamlist.aspx");

        }

       catch
        {
            Label_thanhcong.Text = tbsanphamBUS.error;
       //     Response.Redirect("sanphamlist.aspx");
        }
    }
}
